package com.example.online_order.conrtoller;


import com.example.online_order.pojo.AdminAccount;
import com.example.online_order.pojo.ClientAccount;
import com.example.online_order.pojo.Result;
import com.example.online_order.service.AdminAcountService;
import com.example.online_order.service.ClientAccountService;
import com.example.online_order.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@CrossOrigin
@Slf4j

public class LoginController {


    @Autowired
    AdminAcountService adminAcountService;
    @Autowired
    ClientAccountService clientAccountService;

    @PostMapping("/adlogin")
    public Result login(@RequestBody AdminAccount account) {
        System.out.println(account);
        //取数据库中查找
        AdminAccount accountDb = adminAcountService.Login(account);
        System.out.println(accountDb);

        if(accountDb == null) {
                    return Result.fail("用户不存在！",444);
                }else{
                    //获取token
                    String token = JwtUtils.createToken(accountDb.getId().toString(), accountDb.getUserName());
                    return Result.Login(accountDb,token);


        }

    }

    @PostMapping("/cllogin")
    public Result login(@RequestBody ClientAccount account) {
        Result result = new Result();
        ClientAccount accDb = clientAccountService.login(account);
        if(accDb==null) {
            result.setStatus(444);
            result.setMessage("用户不存在！");
        }else {
            // 登录成功
            String token = JwtUtils.createToken(accDb.getId().toString(), accDb.getUserName());
            return Result.Login(accDb,token);

        }
        return result;
    }
}
